<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }
        
        .wrap {
            width: 900px;
            height: 500px;
            margin: 100px auto 0;
            display: flex;
            justify-content: space-between;
            align-items: center;
            position: relative;
        }
        
        .box {
            position: relative;
            width: 800px;
            height: 100%;
            border: 4px solid red;
        }
        
        .box>li {
            position: absolute;
            width: 100%;
            height: 100%;
            display: none;
        }
        
        .box>li>img {
            width: 100%;
            height: 500px;
            display: inline-block;
            margin: 0 auto;
        }
        
        .nav-btn {
            width: 808px;
            position: absolute;
            display: flex;
            justify-content: space-between;
            top: 50%;
            margin-top: -20px;
        }
        
        .nav-btn a {
            text-decoration: none;
            background: #333;
            color: red;
            font-size: 40px;
            padding: 8px 4px;
            opacity: 0.2;
            cursor: pointer;
        }
        
        .button-btn {
            display: flex;
            justify-content: space-evenly;
            width: 200px;
            position: absolute;
            bottom: 16px;
            left: 50%;
            margin-left: -100px;
        }
        
        .button-btn li {
            width: 24px;
            height: 24px;
            border-radius: 50%;
            text-align: center;
            line-height: 24px;
            background: #ccc;
            cursor: pointer;
        }
        
        .box .show {
            display: block;
        }
        
        .button-btn .active {
            background-color: red;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <ul class="box">
            <li class="show"><img src="img/1.jpg" alt="" /></li>
            <li><img src="img/2.jpg" alt="" /></li>
            <li><img src="img/3.jpg" alt="" /></li>
            <li><img src="img/4.jpg" alt="" /></li>
        </ul>
        <div class="nav-btn">
            <a href="#" class="nav-left">&lt;</a>
            <a href="#" class="nav-right">&gt;</a>
        </div>
        <ul class="button-btn">
            <li class="active">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>
    </div>
    <script>
        //获取元素
        var wrap = document.querySelector(".wrap");
        var li = document.querySelectorAll(".box>li");
        var navBtn = document.querySelectorAll(".nav-btn>a");
        var buttonBtn = document.querySelectorAll(".button-btn>li");
        console.log('wrap:', wrap);

        var cutIndex = 0; //当前图片下标
        var timer = setInterval(slideFun, 2000);

        function slideFun() {
            cutIndex++;
            if (cutIndex > li.length - 1) {
                cutIndex = 0;
            }
            show(cutIndex);
        }

        //渲染
        function show(cutIndex) {
            //所有图片隐藏
            for (var i = 0; i < li.length; i++) {
                li[i].className = "";
                buttonBtn[i].className = "";
            }
            //当前图片	当前按钮
            li[cutIndex].className = "show";
            buttonBtn[cutIndex].className = "active";
        }
        //鼠标移入轮播停止
        wrap.onmouseover = function() {
            for (var i = 0; i < navBtn.length; i++) {
                navBtn[i].style.opacity = "0.8";
            }
            clearInterval(timer);
        }

        wrap.onmouseout = function() {
            for (var i = 0; i < navBtn.length; i++) {
                navBtn[i].style.opacity = "0.2";
            }
            timer = setInterval(slideFun, 2000);
        }


        //左边--
        navBtn[0].onclick = function() {
            cutIndex--;
            if (cutIndex < 0) {
                cutIndex = li.length - 1;
            }
            show(cutIndex);
        }

        //右边++
        navBtn[1].onclick = function() {
            cutIndex++;
            if (cutIndex > li.length - 1) {
                cutIndex = 0;
            }
            show(cutIndex);
        }

        //底部按钮	点击按钮显示对应的图片
        for (var i = 0; i < buttonBtn.length; i++) {
            buttonBtn[i].index = i;
            buttonBtn[i].onclick = function() {
                cutIndex = this.index;
                show(cutIndex);
            }
        }
    </script>
</body>

</html>